/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supportlearning.session;

import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import supportlearning.entities.Account;
import supportlearning.entities.CMSRole;

/**
 *
 * @author Huy
 */
@Stateless
public class edituser implements edituserLocal {

    @PersistenceContext(unitName = "eprojectSupportLearning-ejbPU")
    private EntityManager em;

    public int UpdateUser(String User, String pass, String FirstN, String LastN, String Addr, boolean Gend, Date CDate, String Email, boolean Status, String avatar, int roleid) {
        try {
            CMSRole role = em.find(CMSRole.class, roleid);
            if (role != null) {
                Account acc = em.find(Account.class, User);
                if (acc == null) {
                    return 2;//AccountID loi~
                } else {
                    acc.setPassword(pass);
                    acc.setFirstName(FirstN);
                    acc.setLastName(LastN);
                    acc.setAddress(Addr);
                    acc.setGender(Gend);
                    acc.setCreateDate(CDate);
                    acc.setEmail(Email);
                    acc.setStatus(Status);
                    acc.setAvatar(avatar);
                    acc.setCMSRole(role);
                    em.persist(acc);
                    return 3;//OK
                }
            } else {
                return 1;//RoleID loi~
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 4;
        }
    }

    public void persist(Object object) {
        em.persist(object);
    }
}
